<?php
function db_test_mysql(){
	if(!getToMysql(false)){
		die(log::w('Mysql Connect Failed!'));
	}
}

function db_select_longtime($query) {
    $dbmysql = mysql_pconnect(FROM_DB_MYSQL_HOST . ':' . FROM_DB_MYSQL_PORT, FROM_DB_MYSQL_USER, FROM_DB_MYSQL_PASS);
    mysql_query('use ' . FROM_DB_MYSQL_DBNAME, $dbmysql);
    mysql_query('set names ' . FROM_DB_MYSQL_CHARSET, $dbmysql);
    $qr = mysql_query($query, $dbmysql);
    $r = array();
    if (mysql_num_rows($qr) == 0) {
        return false;
    } else {
        while ($mr = mysql_fetch_assoc($qr)) {
            $r[] = $mr;
        }
    }
    mysql_close($dbmysql);
    return $r;
}

function db_select($query, $flag = false) {
    return from_db_select($query, $flag);
}

function db_query($query) {
    return from_db_query($query, $flag);
}

function getFromMysql($flag = false) {
    if (FROM_DB_MYSQL_HOST && TO_DB_MYSQL_HOST) {
        $new = true;
    } else {
        $new = false;
    }
    static $dbmysql;
    if (!$dbmysql) {
        if (!$flag) {

            $dbmysql = mysql_connect(FROM_DB_MYSQL_HOST . ':' . FROM_DB_MYSQL_PORT, FROM_DB_MYSQL_USER, FROM_DB_MYSQL_PASS, $new);
        } else {
            $dbmysql = mysql_pconnect(FROM_DB_MYSQL_HOST . ':' . FROM_DB_MYSQL_PORT, FROM_DB_MYSQL_USER, FROM_DB_MYSQL_PASS, $new);
        }
        if (!$dbmysql) {
            log::w('ERROR:' . mysql_error());
            return false;
        }
        mysql_query('use ' . FROM_DB_MYSQL_DBNAME, $dbmysql);
        mysql_query('set names ' . FROM_DB_MYSQL_CHARSET, $dbmysql);
    }
    if (!$dbmysql) {
        log::w('ERROR:' . $query);
        log::w('ERROR:' . mysql_error());
        return mysql_error();
    }
    return $dbmysql;
}

function getToMysql($flag = false) {
    if (FROM_DB_MYSQL_HOST && TO_DB_MYSQL_HOST) {
        $new = true;
    } else {
        $new = false;
    }
    static $dbmysql;
    if (!$dbmysql) {
        if (!$flag) {
            $dbmysql = mysql_connect(FROM_DB_MYSQL_HOST . ':' . FROM_DB_MYSQL_PORT, FROM_DB_MYSQL_USER, FROM_DB_MYSQL_PASS, $new);
        } else {
            $dbmysql = mysql_pconnect(FROM_DB_MYSQL_HOST . ':' . FROM_DB_MYSQL_PORT, FROM_DB_MYSQL_USER, FROM_DB_MYSQL_PASS, $new);
        }
        if (!$dbmysql) {
            log::w('ERROR:' . mysql_error());
            return false;
        }
        mysql_query('use ' . TO_DB_MYSQL_DBNAME, $dbmysql);
        mysql_query('set names ' . TO_DB_MYSQL_CHARSET, $dbmysql);
    }
    if (!$dbmysql) {
        log::w('ERROR:' . $query);
        log::w('ERROR:' . mysql_error());
        return mysql_error();
    }
    return $dbmysql;
}

function from_db_select($query, $flag = false) {
    $qr = mysql_query($query, getFromMysql($flag));
    $r = array();
    if (mysql_num_rows($qr) == 0) {
        return false;
    } else {
        while ($mr = mysql_fetch_assoc($qr)) {
            $r[] = $mr;
        }
    }
    return $r;
}

function to_db_select($query, $flag = false) {
    $qr = mysql_query($query, getToMysql($flag));
    $r = array();
    if (mysql_num_rows($qr) == 0) {
        return false;
    } else {
        while ($mr = mysql_fetch_assoc($qr)) {
            $r[] = $mr;
        }
    }
    return $r;
}

function from_db_query($query, $flag = false) {
    $result = mysql_query($query, getFromMysql($flag));
    if (!$result) {
        log::w('ERROR:' . $query);
        log::w('ERROR:' . mysql_error());
        return false;
    }
    return $result;
}

function to_db_query($query, $flag = false) {
    $result = mysql_query($query, getToMysql($flag));
    if (!$result) {
        log::w('ERROR:' . $query);
        log::w('ERROR:' . mysql_error());
        return false;
    }
    return $result;
}

function makeInsertSQL($table, $data) {
    $strKeys = '(`' . implode('`,`', array_keys($data)) . '`)';
    foreach ($data as &$v) {
        $v = str_replace('&nbsp;', '', $v);
        $v = addslashes($v);
    }unset($v);
    $strValues = "('" . implode("','", $data) . "')";
    $query = "INSERT IGNORE INTO `{$table}` " . $strKeys . " VALUES " . $strValues . ';';
    return $query;
}

?>